<template>
  <el-table
    :data="categoryList"
    style="width: 100%"
    :header-cell-style="{ 'text-align': 'center' }"
    :cell-style="{ 'text-align': 'center' }"
  >
    <el-table-column prop="activity_categoryId" label="ID" width="160px" />
    <el-table-column
      prop="activity_categoryName"
      label="活动类型名称"
      width="160px"
    />
    <el-table-column prop="activity_categoryDes" label="描述" />
    <el-table-column label="状态" width="200px">
      <template #default="scope">
        <el-button
          type="success"
          size="small"
          v-if="scope.row.activity_categoryState === '1'"
          @click="changeState(scope.row, '2')"
          >开启</el-button
        >
        <el-button
          type="warning"
          size="small"
          v-if="scope.row.activity_categoryState === '2'"
          @click="changeState(scope.row, '1')"
          >关闭</el-button
        >
        <el-button type="primary" size="small" @click="handleMod(scope.row)"
          >修改</el-button
        >
        <el-button type="danger" size="small" @click="handleDel(scope.row)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <div class="container-pagination">
    <el-pagination
      v-model:currentPage="pagination.currentPage"
      v-model:page-size="pagination.pageSize"
      :total="pagination.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[1, 2, 3, 4, 5, 6, 7]"
      layout="total, sizes, prev, pager, next, jumper"
    />
  </div>
  <CategoryMod ref="categoryMod"></CategoryMod>
</template>

<script setup>
import { getCurrentInstance } from 'vue'
import { useStore } from 'vuex'
import useGetters from '@/hooks/useGetters'
import CategoryMod from './CategoryMod'
const poxy = getCurrentInstance()
const store = useStore()
const { dispatch, commit } = store
const activityGettets = useGetters(['categoryPagination', 'categoryList'])
const { categoryPagination: pagination, categoryList } = activityGettets
// 初始化
const getCategoryList = () => {
  dispatch('activity/getCategoryList', { pagination })
}
getCategoryList()
// 每页数量改变的方法
const handleSizeChange = (val) => {
  pagination.value.pageSize = val
  getCategoryList()
}
// 页数改变的方法
const handleCurrentChange = (val) => {
  pagination.value.currentPage = val
  getCategoryList()
}
// 改变状态
const changeState = async (category, state) => {
  await dispatch('activity/updateCategory', {
    activity_categoryId: category.activity_categoryId,
    activity_categoryState: state
  }).then(() => {
    getCategoryList()
  })
}
// 修改按钮
const handleMod = async (category) => {
  commit('activity/SAVECATEGORY', category)
  poxy.refs.categoryMod.changeDialogFormVisible()
}
// 删除按钮
const handleDel = async (category) => {
  await dispatch('activity/updateCategory', {
    activity_categoryId: category.activity_categoryId,
    activity_status: 0
  }).then(() => {
    getCategoryList()
  })
}
</script>

<style lang="scss" scoped></style>
